#include "tcpclient.h"
#include "ui_tcpclient.h"
#include "protocol.h"

#include<QByteArray>
#include<QMessageBox>
#include<QHostAddress>

tcpClient::tcpClient(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::tcpClient)
{
    ui->setupUi(this);

    resize(400,200);

    loadConfig();

    connect(&m_tcpSocket, SIGNAL(connected()), this, SLOT(showConnect()));
    connect(&m_tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg()));

    m_tcpSocket.connectToHost(QHostAddress(m_ip), m_port);
}

tcpClient::~tcpClient()
{
    delete ui;
}

void tcpClient::loadConfig()
{
    QFile file(":/client.config");
    if(file.open(QIODeviceBase::ReadOnly))
    {
        QByteArray baData = file.readAll();
        QString strData = baData.toStdString().c_str();

        strData.replace("\r\n", " ");
        QStringList strDataList = strData.split(" ");
        m_ip = strDataList.at(0);
        m_port = strDataList.at(1).toUShort();
        qDebug()<<"ip:"<<m_ip<<" port:"<<m_port;

        file.close();
    }
    else
    {
        QMessageBox::critical(this, "open config", "open config fail");
    }
}

void tcpClient::showConnect()
{
    QMessageBox::information(this, "连接服务器", "连接服务器成功");
}

void tcpClient::recvMsg()
{
    qDebug()<<m_tcpSocket.bytesAvailable();
    uint uiPDULen = 0;
    m_tcpSocket.read((char*)&uiPDULen, sizeof(uint));
    qDebug()<<uiPDULen;
    uint uiMsgLen = uiPDULen - sizeof(PDU);
    PDU *pdu = mkPDU(uiMsgLen);
    m_tcpSocket.read((char*)pdu+sizeof(uint), uiPDULen-sizeof(uint));
    qDebug()<<pdu->uiMsgType<<(char*)(pdu->caMsg);

    switch(pdu->uiMsgType)
    {
    case ENUM_MSG_TYPE_REGIST_RESPOND:
    {
        if( 0 == strcmp(pdu->caData, REGIST_OK))
        {
            QMessageBox::information(this, "注册", REGIST_OK);
        }else if( 0 == strcmp(pdu->caData, REGIST_FAILED))
        {
            QMessageBox::warning(this, "注册", REGIST_FAILED);
        }
    }
    case ENUM_MSG_TYPE_LOGIN_RESPOND:
    {
        if( 0 == strcmp(pdu->caData, LOGIN_OK))
        {
            QMessageBox::information(this, "登录", LOGIN_OK);
            OpeWidget::getInstance().show();
            this->hide();

        }else if( 0 == strcmp(pdu->caData, LOGIN_FAILED))
        {
            QMessageBox::warning(this, "登录", LOGIN_FAILED);
        }

    }
    default:
        break;

    }
    free(pdu);
    pdu = NULL;
}

#if 0
void tcpClient::on_send_pd_clicked()
{
    QString strMsg = ui->lineEdit->text();
    if(strMsg.isEmpty())
    {
        QMessageBox::warning(this, "消息提示", "消息不能为空");
    }
    else
    {
        PDU* pdu = mkPDU(strMsg.size());
        pdu->uiMsgType = 8888;
        memcpy(pdu->caMsg, strMsg.toStdString().c_str(), strMsg.size());
        m_tcpSocket.write((char*)pdu, pdu->uiPDULen);
        free(pdu);
        pdu=NULL;
    }
}
#endif


void tcpClient::on_cancel_pb_clicked()
{

}


void tcpClient::on_login_pb_clicked()
{
    QString strName = ui->userName_le->text();
    QString strPwd = ui->pwd_le->text();

    if(!strName.isEmpty() && !strPwd.isEmpty())
    {
        PDU* pdu = mkPDU(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_LOGIN_REQUEST;
        strncpy(pdu->caData, strName.toStdString().c_str(),32);
        strncpy(pdu->caData+32, strPwd.toStdString().c_str(),32);
        m_tcpSocket.write((char*)pdu, pdu->uiPDULen);
        free(pdu);
        pdu=NULL;
    }
    else
    {
        QMessageBox::critical(this, "登录", "登录失败：用户名或者密码为空");
    }
}


void tcpClient::on_regist_pb_clicked()
{
    QString strName = ui->userName_le->text();
    QString strPwd = ui->pwd_le->text();

    if(!strName.isEmpty() && !strPwd.isEmpty())
    {
        PDU* pdu = mkPDU(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_REGIST_REQUEST;
        strncpy(pdu->caData, strName.toStdString().c_str(),32);
        strncpy(pdu->caData+32, strPwd.toStdString().c_str(),32);
        m_tcpSocket.write((char*)pdu, pdu->uiPDULen);
        free(pdu);
        pdu=NULL;
    }
    else
    {
        QMessageBox::critical(this, "注册", "注册失败：用户名或者密码为空");
    }

}

